/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DA;

import Control.DBConnector;
import Entity.Customer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author theInterloper
 */
public class customerDA {

    public static void write(Customer customer) throws SQLException {
        
        int ID =            customer.getID();
        String USERNAME =   customer.getUSERNAME();
        String FIRSTNAME =  customer.getFIRSTNAME();
        String SECONDNAME = customer.getSECONDNAME();
        String EMAIL =      customer.getEMAIL();
        String PASSWORD =   customer.getPASSWORD();
        String ADDRESS1 =   customer.getADDRESS1();
        String ADDRESS2 =   customer.getADDRESS2();
        String ADDRESS3 =   customer.getADDRESS3();
        String PHONE =      customer.getPHONE();
        
        String insertArtist = "INSERT INTO customer (customerUserName,customerFirstName,customerSecondName,customerEmail,customerPassword,customerAddress1,customerAddress2,customerAddress3,customerPhoneNumber) VALUES "
                + "('"+ USERNAME + "','" + FIRSTNAME + "','" + SECONDNAME + "','" + EMAIL + "','" + PASSWORD + "','" + ADDRESS1 + "','" + ADDRESS2 + "','" + ADDRESS3 + "','" + PHONE + "');";
        
        DBConnector dbconnect = new DBConnector();
        Connection connect = dbconnect.setUpConnection();
        Statement statement = connect.createStatement();
        statement.execute(insertArtist);

    }

    public static Customer read(int i) throws SQLException {
         
        String getByID = "select * from customer where customerID = '" + i + "'";
        
        DBConnector dbconnect = new DBConnector();
        Connection connect = dbconnect.setUpConnection();
        Statement statement = connect.createStatement();
         
        ResultSet resultset = statement.executeQuery(getByID);
        
        Customer customer = new Customer();
        
        while(resultset.next()){
            customer.setID(resultset.getInt(1));
            customer.setUSERNAME(resultset.getString(2));
            customer.setFIRSTNAME(resultset.getString(3));
            customer.setSECONDNAME(resultset.getString(4));
            customer.setEMAIL(resultset.getString(5));
            customer.setPASSWORD(resultset.getString(6));
            customer.setADDRESS1(resultset.getString(7));
            customer.setADDRESS2(resultset.getString(8));
            customer.setADDRESS3(resultset.getString(9));
            customer.setPHONE(resultset.getString(10));
        } 
        return customer;
    }

    public static Customer readByUserName(String userName) throws SQLException {
        String getByUserName = "select * from customer where customerUserName = '" + userName + "'";
        
        DBConnector dbconnect = new DBConnector();
        Connection connect = dbconnect.setUpConnection();
        Statement statement = connect.createStatement();
         
        ResultSet resultset = statement.executeQuery(getByUserName);
        
        Customer customer = new Customer();
        
        while(resultset.next()){
            customer.setID(resultset.getInt(1));
            customer.setUSERNAME(resultset.getString(2));
            customer.setFIRSTNAME(resultset.getString(3));
            customer.setSECONDNAME(resultset.getString(4));
            customer.setEMAIL(resultset.getString(5));
            customer.setPASSWORD(resultset.getString(6));
            customer.setADDRESS1(resultset.getString(7));
            customer.setADDRESS2(resultset.getString(8));
            customer.setADDRESS3(resultset.getString(9));
            customer.setPHONE(resultset.getString(10));
        } 
        return customer;
    }
    
    public static Customer readByID(int id) throws SQLException {
        String getByUserId = "select * from customer where customerID = '" + id + "'";
        
        DBConnector dbconnect = new DBConnector();
        Connection connect = dbconnect.setUpConnection();
        Statement statement = connect.createStatement();
         
        ResultSet resultset = statement.executeQuery(getByUserId);
        
        Customer customer = new Customer();
        
        while(resultset.next()){
            customer.setID(resultset.getInt(1));
            customer.setUSERNAME(resultset.getString(2));
            customer.setFIRSTNAME(resultset.getString(3));
            customer.setSECONDNAME(resultset.getString(4));
            customer.setEMAIL(resultset.getString(5));
            customer.setPASSWORD(resultset.getString(6));
            customer.setADDRESS1(resultset.getString(7));
            customer.setADDRESS2(resultset.getString(8));
            customer.setADDRESS3(resultset.getString(9));
            customer.setPHONE(resultset.getString(10));
        } 
        return customer;
    }
    
}
